/**
 * 
 */
package com.taobao.wolfwolf.util;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Tux
 *
 * 创建时间: 2011-5-30 上午10:57:37
 */
public class JSONObject {
	private LinkedHashMap<Object, Object> map = new LinkedHashMap<Object, Object>();
	public JSONObject() {
	}
	
	public void put(Object key, Object value) {
		map.put(key, value);
	}
	
	public String toJSONString() {
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("{");
		for(Map.Entry<Object, Object> m : map.entrySet()) {
			Object o = m.getValue();
			String key = (String) m.getKey();

			buffer.append("\"" + key + "\": ");
			
			
			if (o instanceof JSONObject) {
				String temp = ((JSONObject) o).toJSONString();
				buffer.append(temp);
			} else if (o instanceof JSONArray) {
				String temp = ((JSONArray) o).toJSONString();
				buffer.append(temp);
			} else if(o instanceof String) {
				buffer.append("\"" + o + "\"");
			} else if(o instanceof Integer || o instanceof Float || o instanceof Double || o instanceof Long) {
				buffer.append(o);
			} else if(o == null) {
				buffer.append("\"\"");
			}
			buffer.append(",");
		}
		buffer.append("}");
		buffer.deleteCharAt(buffer.length() - 2);
		
		return buffer.toString();
	}
}
